<?php

add_action('widgets_init', 'register_browse_by_location_widget_job');
function register_browse_by_location_widget_job() {
	register_widget('PricerrTheme_browse_by_location');
}

class PricerrTheme_browse_by_location extends WP_Widget {

	function PricerrTheme_browse_by_location() {
		$widget_ops = array( 'classname' => 'browse-by-location', 'description' => 'Show all locations and browse by location' );
		$control_ops = array( 'width' => 200, 'height' => 250, 'id_base' => 'browse-by-location' );
		$this->WP_Widget( 'browse-by-location', 'PricerrTheme - Browse by Location', $widget_ops, $control_ops );
	}

	function widget($args, $instance) {
		extract($args);
		
		$loc_per_row = $instance['loc_per_row'];
		$widget_id = $args['widget_id'];
		
		
				echo $before_widget;
		
		if ($instance['title']) echo $before_title . apply_filters('widget_title', $instance['title']) . $after_title;
		
		$loc_per_row = $instance['loc_per_row'];
		$widget_id = $args['widget_id'];
		
		$nr = 1;
		$categ = "location";
		
		if(!empty($loc_per_row)) $nr = $loc_per_row;
		echo '<style>#'.$widget_id.' #location-stuff li ul { width: '.round(100/$nr).'%}</style>';
		
		
         $terms = get_terms("job_location","parent=0&hide_empty=0");
		 $count = count($terms); $i = 0;
		 if ( $count > 0 ){
		     echo "<ul id='location-stuff'>";
		     foreach ( $terms as $term ) {
		       
			   if($i%$nr == 0) echo "<li>";
		       
			   	$terms2 = get_terms("job_location","parent=".$term->term_id."&hide_empty=0");
		       	
					echo '<ul>';
					echo "
		       		<h3><a href='".PricerrTheme_get_browse_jobs_link($categ, $term->slug)."'>" . $term->name."</a></h3>";
			   
			   if($terms2)
				{
					foreach ( $terms2 as $term2 ) 
		       			echo "<li><a href='".PricerrTheme_get_browse_jobs_link($categ, $term2->slug)."'>" . $term2->name."</a></li>";
				}
					echo '</ul>';
				
				
		       if(($i+1) % $nr == 0) echo "</li>";
			   
			   $i++;
		        
		     }
				
				if(($i+1) % $nr != 0) echo "</li>";


		     echo "</ul>";
		 }           
				
		echo $after_widget;
		
		
		
	}

	function update($new_instance, $old_instance) {
	
		return $new_instance;
	}

	function form($instance) { ?>
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title'); ?>:</label>
			<input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" 
			value="<?php echo esc_attr( $instance['title'] ); ?>" style="width:95%;" />
		</p>
		
		
		<p>
			<label for="<?php echo $this->get_field_id('loc_per_row'); ?>"><?php _e('Locations per row'); ?>:</label>
			<input type="text" id="<?php echo $this->get_field_id('loc_per_row'); ?>" name="<?php echo $this->get_field_name('loc_per_row'); ?>" 
			value="<?php echo esc_attr( $instance['loc_per_row'] ); ?>" style="width:20%;" />
		</p>
		
					
	<?php 
	}
}



?>